<?php
// +----------------------------------------------------------------------
// | 中联致远服务端框架
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://www.zhongliansoft.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: tony <tanyu@zhongliansoft.com>
// +----------------------------------------------------------------------

namespace app\Manage\controller;

use app\common\controller\Manage;
use app\common\model\BookInfo as BookInfoModel;
use app\common\model\BookPlan as BookPlanModel;
use app\common\model\StudentTypeBookRel as StudentTypeBookRelModel;
use think\facade\Request;
use think\Db;

class Book extends Manage
{
    /**
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        if(Request::isAjax())
        {
            $bookInfoModel = new BookInfoModel();
            return $bookInfoModel->tableData(input('param.'));
        }
        return $this->fetch();
    }

    public function getBookInfo()
    {
        $result = [
            'status' => false,
            'msg' => '获取教材信息失败',
            'data' => []
        ];

        $id  = input('post.id', '');

        if (!$id) {
            return $result;
        }

        $bookInfoModel = new BookInfoModel();
        $bookInfo = $bookInfoModel->where(['id' => $id])->find();
        
        if ($bookInfo) {  
            $result['status'] = true;
            $result['data'] = $bookInfo;
            $result['msg'] = '获取教材信息成功';  
        }

        return $result;
    }
    
    public function getBookPrice()
    {
        $result = [
            'status' => false,
            'msg' => '获取教材信息失败',
            'data' => []
        ];

        $id  = input('post.id', '');

        if (!$id) {
            return $result;
        }

        $bookInfoModel = new BookInfoModel();
        $price = $bookInfoModel->getBookPrice($id);
        
        if ($price) {  
            $result['status'] = true;
            $result['data'] = $price;
            $result['msg'] = '获取教材信息成功';  
        }

        return $result;
    }
    
    /**
     * 教材增加
     * @return array|mixed
     */
    public function addBookInfo()
    {
        if (Request::isPost()) {
            $input     = Request::param();
            $bookInfoModel = new BookInfoModel();

            // 判断是否有同名的名称和ISBN
            // $data1['book_name'] = $input['book_name'];
            // $data1 = $bookInfoModel->tableData($data1);
  
            // if( $data1['count'] > 0) { 
            //     return [
            //         'status' => false,
            //         'data' => '',
            //         'msg' => '已存在相同的教材名称！'
            //     ];
            // }

            $data2['book_isbn'] = $input['book_isbn'];
            $data2 = $bookInfoModel->tableData($data2);
  
            if( $data2['count'] > 0) { 
                return [
                    'status' => false,
                    'data' => '',
                    'msg' => '已存在相同的ISBN！'
                ];
            }

            $result    = $bookInfoModel->addBookInfo($input);
            return $result;
        }
        
        return $this->fetch('addBookInfo');
    }

    /**
     * 编辑教材
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function editBookInfo()
    { 
        $bookInfoModel = new BookInfoModel();
        $input     = Request::param();
        if (Request::isPost()) {
            // 判断是否有同名的名称和ISBN
            // $bookInfo_info = $bookInfoModel->where(['book_name'=>$input['book_name']])->where('id','neq',$input['id'])->find();
            // if($bookInfo_info){
            //     return [
            //         'status' => false,
            //         'data' => '',
            //         'msg' => '已存在相同的教材名称！'
            //     ];
            // }

            $bookInfo_info = $bookInfoModel->where(['book_isbn'=>$input['book_isbn']])->where('id','neq',$input['id'])->find();
            if($bookInfo_info){
                return [
                    'status' => false,
                    'data' => '',
                    'msg' => '已存在相同的ISBN！'
                ];
            }
 
            $result    = $bookInfoModel->editBookInfo($input);
            return $result;
        }
  
        $info = $bookInfoModel->where(['id'=>$input['id']])->find();

        $this->assign('info', $info);
        return $this->fetch('editBookInfo');
    }

    /**
     *  删除教材
     * User:tony
     * @return array
     */
    public function delBookInfo()
    {
        $result = [
            'status' => false,
            'msg' => '删除失败',
            'data' => []
        ];

        $input     = Request::param();
        $id  = $input['id'];

        if (!$id) {
            return $result;
        }

        // 判断当前教材是否使用
        $bookPlanModel = new BookPlanModel();
        $info = $bookPlanModel->where(['plan_book_id'=>$id])->find();
         
        if (!$info) { 
            // 判断是否在非计划发放中关联
            $studentTypeBookRelModel = new StudentTypeBookRelModel();
            $extraInfo = $studentTypeBookRelModel->where(['book_id'=>$id])->find(); 

            if (!$extraInfo) { 
                $bookInfoModel = new BookInfoModel();
                if ($bookInfoModel->where(['id' => $id])->delete()) {
                    $result['status'] = true;
                    $result['msg'] = '删除成功';
                }
            } else { 
                $result['msg']    = '当前教材已在非计划中使用无法删除！';
            }
        } else { 
            $result['msg']    = '当前教材已在计划中使用无法删除！';
        }

        return $result;
    }

    /**
     * 导入教材
     * @return array|mixed
     */
    public function importBookInfo()
    {
        if (Request::isPost()) {
            return null;
        }
        return $this->fetch('importBookInfo');
    }
    
    /**
     * 导入教材定价信息
     * @return array|mixed
     */
    public function importBookPrice()
    {
        if (Request::isPost()) {
            return null;
        }
        return $this->fetch('ImportBookPrice');
    }
}